package com.anjoyo.xgh.musicshare.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Handler;

public class BitmapUtil {

	/**
	 * 把一个bitmap写入到本地sdcard
	 * 
	 * @param url
	 * @param bitmap
	 */
	public static void saveImg(String url, Bitmap bitmap) {
		File file = new File(url);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		FileOutputStream os;
		try {
			os = new FileOutputStream(file);
			bitmap.compress(CompressFormat.PNG, 100, os);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据URL 下载一张网络图片
	 * 
	 * @param url
	 * @return
	 */
	public static Bitmap downbitmap(String url) {
		Bitmap bitmap = null;
		try {
			HttpURLConnection connection = (HttpURLConnection) new URL(url)
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			bitmap = BitmapFactory.decodeStream(connection.getInputStream());
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	/**
	 * 保存一张网络图片到本地
	 * 
	 * @param url
	 * @param filepath
	 */
	public static void SaveNetImg(String url, String filepath) {
		Bitmap bitmap = downbitmap(url);
		saveImg(filepath, bitmap);
	}

	public static void saveNetImg(final String url, final String filepath,
			final Handler handler) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("url是：" + url);
				Bitmap bitmap = downbitmap(url);
				// System.out.println("bitmap大小："+bitmap.getByteCount());
				saveImg(filepath, bitmap);
				handler.sendEmptyMessage(Constant.DOWLOAD_OK);
			}
		}).start();
	}

	public static Bitmap getUserIcon(String url) {
		String path = Constant.NOCALEAR_CACHE + MD5Util.MD5(url) + ".jpg";
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		// 新浪用户注册头像
		if (url.startsWith("http:")) {
		} else {
			url = Constant.SERVER_IP + url;
		}
		// 判断这个url的图片有没有在本地缓存过，如果有直接返回这个图片
		if (file.exists()) {
			return BitmapFactory.decodeFile(file.getAbsolutePath());
		}
		InputStream is = null;
		HttpURLConnection connection = null;
		try {
			URL urL = new URL(url);
			connection = (HttpURLConnection) urL.openConnection();
			connection.setDoInput(true);
			connection.connect();
			is = connection.getInputStream();
			// 把bitmap保存
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			saveImg(file.getAbsolutePath(), bitmap);
			return bitmap;
		} catch (Exception e) {
			
		}
		return null;
	}
}
